package com.walk.zworld.ui.activity.my

import android.annotation.SuppressLint
import android.os.Build
import android.text.Html
import androidx.annotation.RequiresApi
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.walk.zworld.R
import com.walk.zworld.db.model.ReadRecordModel
import com.walk.zworld.mvvm.vm.RoomViewModel
import com.walk.zworld.view.MyDialog
import com.walk.zworld.view.SwipeItemLayout
import com.walk.zworld.webView.WebViewActivity
import com.base.lib.base.utils.ThreadUtils
import com.base.lib.base.view.BaseMvvmListActivity
import java.text.SimpleDateFormat

class ReadRecordActivity : BaseMvvmListActivity<Any, RoomViewModel, ReadRecordActivity.ReadRecordAdapter>() {

    private lateinit var clearDialog: MyDialog

    override fun onBindAdapter(): Class<ReadRecordAdapter> = ReadRecordAdapter::class.java

    override fun onBindViewModel(): Class<RoomViewModel> = RoomViewModel::class.java

    override fun initView() {
        mRecyclerView.run {
            addOnItemTouchListener(SwipeItemLayout.OnSwipeItemTouchListener(this@ReadRecordActivity))
        }
        mAdapter.run {
            addChildClickViewIds(R.id.rl_read, R.id.btn_delete_read)
            setOnItemChildClickListener { adapter, view, position ->
                val any = data[position]
                when (view.id) {
                    R.id.rl_read -> WebViewActivity.start(
                        this@ReadRecordActivity,
                        if (any is ReadRecordModel) any.link else ""
                    )
                    R.id.btn_delete_read -> {
                        mViewModel.removeRecord(if (any is ReadRecordModel) any.link else "")
                            .observe(this@ReadRecordActivity) {
                                adapter.removeAt(position)
                            }
                    }
                }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initData() {
        setTop("阅读历史")
        setRight("清空") {
            if (!this::clearDialog.isInitialized) {
                clearDialog = MyDialog(this)
                clearDialog.run {
                    setDialogText("清空后无法还原,确认全部删除吗")
                    setClickListener { v ->
                        when (v.id) {
                            R.id.tv_dialog_sure -> {
                                if (clearDialog.isShowing) clearDialog.dismiss()
                                mViewModel.removeAllRecord().observe(this@ReadRecordActivity) {
                                    mAdapter.setList(mutableListOf())
                                    showNoDataView()
                                }
                            }
                            R.id.tv_dialog_cancle -> if (clearDialog.isShowing) clearDialog.dismiss()
                        }
                    }
                    show()
                }
            } else clearDialog.show()
        }
        onRefreshEvent()
    }


    private var page = 0;
    private var firstLoad = true
    override fun onRefreshEvent() {
        mAdapter.loadMoreModule.isEnableLoadMore = false
        isRefresh = true
        // 为了展示骨架屏
        if (firstLoad) {
            firstLoad = false
            ThreadUtils.runOnUiThread({ onRefreshEvent() }, 1000)
        } else {
            val from = if (page == 0) 0 else page * mViewModel.pageSize
            mViewModel.getRecordList(from,mViewModel.pageSize)
        }
    }

    override fun onLoadMoreEvent() {
        isRefresh = false
        mRefreshLayout.finishRefresh()
        page++;
        val from = if (page == 0) 0 else page * mViewModel.pageSize
        mViewModel.getRecordList(from,mViewModel.pageSize)
    }

    class ReadRecordAdapter :
        BaseQuickAdapter<Any, BaseViewHolder>(R.layout.item_read_list),
        LoadMoreModule {

        private lateinit var mSimpleDateFormat: SimpleDateFormat

        @SuppressLint("SimpleDateFormat")
        override fun convert(holder: BaseViewHolder, item: Any) {
            if (item is ReadRecordModel){
                mSimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                holder.setText(R.id.tv_read_title, Html.fromHtml(item.title))
                    .setText(R.id.tv_read_time, mSimpleDateFormat.format(item.time))
            }
        }
    }
}